﻿namespace GoogleMusicDesktop
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;

    internal class StringHelper
    {
        public static string getFileLengthLevel(long fileLength, int pointPostion)
        {
            float num = Convert.ToSingle(fileLength);
            if (fileLength < 0L)
            {
                return "未知";
            }
            if ((fileLength >= 0L) && (fileLength <= 0x3ffL))
            {
                return (num.ToString("N0") + " B");
            }
            if ((fileLength >= 0x400L) && (fileLength <= 0xfffffL))
            {
                float num2 = num / 1024f;
                return (num2.ToString("N" + pointPostion.ToString()) + " KB");
            }
            if ((fileLength >= 0x100000L) && (fileLength <= 0x3fffffffL))
            {
                float num3 = (num / 1024f) / 1024f;
                return (num3.ToString("N" + pointPostion.ToString()) + " MB");
            }
            float num4 = ((num / 1024f) / 1024f) / 1024f;
            return (num4.ToString("N" + pointPostion.ToString()) + " GB");
        }

        public static string GetMiddleString(string source, string strHead, string strTail)
        {
            int index = source.IndexOf(strHead);
            if (index < 0)
            {
                return string.Empty;
            }
            int num2 = source.IndexOf(strTail, index);
            string str = string.Empty;
            if ((index > 0) && (num2 > 0))
            {
                str = source.Substring(index + strHead.Length, (num2 - index) - strHead.Length);
            }
            return str;
        }

        public static string GetSongDownloadUrl(string songId)
        {
            string urlData = HttpHelper.GetUrlData(string.Format("http://www.google.cn/music/top100/musicdownload?id={0}", songId));
            string strHead = "/music/top100/url?q=";    //"<br>\n<a href=\"";
            string strTail = "&ct=rdl";                 //"\"><img";
            return GetMiddleString(urlData, strHead, strTail).Replace("%3A", ":").Replace("%2F", "/");
        }

        public static SongInfo GetSongInfo(string htmlStr)
        {
            SongInfo info = new SongInfo();
            string str = " BottomBorder";
            int index = htmlStr.IndexOf(string.Format("<td class=\"Checkbox{0}\">", str));
            int num2 = 0;
            if (index < 0)
            {
                str = string.Empty;
                index = htmlStr.IndexOf("<td class=\"Checkbox\">");
                if (index < 0)
                {
                    return null;
                }
            }
            string str2 = "id=\"";
            string str3 = "\" value";
            index = htmlStr.IndexOf(str2, index);
            if (index < 0)
            {
                return null;
            }
            num2 = htmlStr.IndexOf(str3, index);
            info.Id = htmlStr.Substring(index + str2.Length, (num2 - index) - str2.Length);
            string str4 = string.Format("<td class=\"Title{0}\">", str);
            string str5 = "</td>";
            index = htmlStr.IndexOf(str4, index);
            if (index >= 0)
            {
                num2 = htmlStr.IndexOf(str5, index);
                info.Title = GetTagValue(htmlStr.Substring(index + str4.Length, (num2 - index) - str4.Length), "a");
            }
            string str6 = string.Format("<td class=\"Artist{0}\">", str);
            string str7 = "</td>";
            index = htmlStr.IndexOf(str6, index);
            if (index >= 0)
            {
                num2 = htmlStr.IndexOf(str7, index);
                info.Artist = GetTagValue(GetTagValue(htmlStr.Substring(index + str6.Length, (num2 - index) - str6.Length), "a"), "span");
            }
            string str8 = string.Format("<td class=\"Album{0}\">", str);
            string str9 = "</td>";
            index = htmlStr.IndexOf(str8, index);
            if (index >= 0)
            {
                num2 = htmlStr.IndexOf(str9, index);
                info.Album = GetTagValue(htmlStr.Substring(index + str8.Length, (num2 - index) - str8.Length), "a");
            }
            return info;
        }

        public static string GetSongListString(string source)
        {
            string strHead = "<table id=\"song_list\"";
            string strTail = "</table>";
            return GetMiddleString(source, strHead, strTail);
        }

        public static string[] GetSongListStringArray(string songListString)
        {
            List<string> list = new List<string>();
            string str = "<tr";
            string str2 = "</tr>";
            int startIndex = 0;
            int index = 0;
            while (true)
            {
                do
                {
                    startIndex = songListString.IndexOf(str, index);
                    if (startIndex < 0)
                    {
                        return list.ToArray();
                    }
                    index = songListString.IndexOf(str2, startIndex);
                }
                while (index < 0);
                string item = songListString.Substring(startIndex, (index - startIndex) + str2.Length);
                if (item.Contains("class=\"SongItem"))
                {
                    list.Add(item);
                }
            }
        }

        public static string GetTagValue(string source, string tag)
        {
            try
            {
                string str = "<" + tag;
                string str2 = ">";
                string str3 = string.Format("</{0}>", tag);
                int index = source.IndexOf(str);
                int num2 = source.IndexOf(str2, index);
                string str4 = string.Empty;
                if ((index >= 0) && (num2 >= 0))
                {
                    index = num2;
                    num2 = source.IndexOf(str3, index);
                    if (num2 >= 0)
                    {
                        str4 = source.Substring(index + str2.Length, (num2 - index) - str2.Length);
                    }
                }
                StringBuilder builder = new StringBuilder(str4);
                builder.Replace("<b>", string.Empty);
                builder.Replace("</b>", string.Empty);
                return builder.ToString();
            }
            catch
            {
                return source;
            }
        }
    }
}

